import { ToggleContainer } from 'cc';
import { List } from '../../Basic/Component/List';
import { PoolFactory } from '../../Basic/Component/PoolFactory';
import { DataMgr, GameData } from '../../Basic/Manager/DataManager';
import { PanelModel } from '../../Basic/UI/PanelModel';
import { GameParams } from '../../Basic/Config/DataConfig';

export class HandbookModel extends PanelModel {
  className: string = 'HandbookModel';
  loadRes: string = 'maingame/handbook/HandbookPanel';

  itemList: List;
  poolFac: PoolFactory;
  togCtr: ToggleContainer;

  curTag: string;

  initItemList() {
    this.curTag = '';
    this.changeItemList('color');
  }

  changeItemList(key: string) {
    console.log('changeItemList', key);

    if (key === this.curTag) return;
    this.itemList.enabled = false;
    const bookList = DataMgr.tables.TbBook.getDataList();
    const data = bookList.filter((book) => book.config === key)[0];
    const gameLevel = GameData.get(GameParams.gameLevel);
    const dataArr = [];
    switch (key) {
      case 'color':
        for (let i = 0; i < data.bookItem.length; i++) {
          dataArr.push({ colorId: data.bookItem[i].itemId, lock: data.bookItem[i].unlockLevel > gameLevel });
        }
        break;
      case 'visitor':
        for (let i = 0; i < data.bookItem.length; i++) {
          dataArr.push({ visitorId: data.bookItem[i].itemId, lock: data.bookItem[i].unlockLevel > gameLevel });
        }
        break;
    }
    this.itemList.setData(dataArr);
    this.itemList.enabled = true;
  }
}
